﻿using d1Common.Interface;
using Newtonsoft.Json;
using System;
using d1Project.Modules.Common.doCache.Domain;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using d1Project.Modules.Common.doCache.Models;
using d1Common;

namespace d1Project.Modules.Common.doCache.Services
{
    public class doCacheServiceRedis : IDoCache
    {
        private string Ip = null;
        private string Port = null;
        private string Password = null;
        RedisMgr redisHelper;
        private Dictionary<string, DoCache> dictCacheData;
        private object lockData;
        private bool initilized = false;
        public doCacheServiceRedis()
        {
            this.dictCacheData = new Dictionary<string, DoCache>();
            this.lockData = new object();
            this.Ip = doServices.Instance.DoConfig.readConfigStringValue(doCacheModule.ModuleIdDefine, "Redis", "Ip", null);
            if (this.Ip == null)
            {
                throw new Exception("配置文件中未定义Ip");
            }
            this.Port = doServices.Instance.DoConfig.readConfigStringValue(doCacheModule.ModuleIdDefine, "Redis", "Port", null);
            if (this.Port == null)
            {
                throw new Exception("配置文件中未定义Port");
            }
            this.Password = doServices.Instance.DoConfig.readConfigStringValue(doCacheModule.ModuleIdDefine, "Redis", "Password", null);
            if (this.Password == null)
            {
                throw new Exception("配置文件中未定义Password");
            }
        }

        private string buildUniqueKey(string _group, string _key)
        {
            return _group + "$_$" + _key;
        }

        public bool ContainsKey(string group, string key)
        {
            string _uniqueKey = this.buildUniqueKey(group, key);
            bool _exist = this.dictCacheData.ContainsKey(_uniqueKey);
            if (!_exist) return false;
            return this.dictCacheData[_uniqueKey].ExpiredTime >= DateTime.Now;
        }

        public T GetData<T>(string group, string key)
        {
            string _uniqueKey = this.buildUniqueKey(group, key);
            var u= redisHelper.Get<T>(_uniqueKey);
            return JsonConvert.DeserializeObject<T>(u.ToString());
        }

        public void InitData()
        {
            if (this.initilized) return;
            this.initilized = true;
            redisHelper= new RedisMgr(this.Ip,this.Port,this.Password);

        }

        public void RemoveData(string group, string key)
        {
            string _uniqueKey = this.buildUniqueKey(group, key);
            lock (this.lockData)
            {
                try
                {
                    if (this.dictCacheData.ContainsKey(_uniqueKey))
                    {
                        redisHelper.Remove(_uniqueKey);
                    }
                }
                catch{};
            }
        }

        public void SetData(string group, string key, object data)
        {
            this.SetData(group, key, data, -1);
        }

        public void SetData(string group, string key, object data, int cacheTime)
        {
            string _value = JsonConvert.SerializeObject(data);
            string _uniqueKey = this.buildUniqueKey(group, key);
            lock (this.lockData)
            {
                try
                {
                    if (this.dictCacheData.ContainsKey(_uniqueKey))
                    {
                        DoCache newDoCache = this.dictCacheData[_uniqueKey];
                        newDoCache.Value = _value;
                        if (cacheTime < 0)
                        {
                            newDoCache.ExpiredTime = DateTime.MaxValue;
                        }
                        else
                        {
                            newDoCache.ExpiredTime = DateTime.Now.AddSeconds(cacheTime);
                        }
                        redisHelper.Remove(_uniqueKey);//先删除
                        redisHelper.Set<string>(newDoCache.Id, newDoCache.Value);//设置缓存
                        this.dictCacheData[_uniqueKey] = newDoCache;

                    }
                    else
                    {
                        DoCache newDoCache = new DoCache();
                        newDoCache.Id = _uniqueKey;
                        newDoCache.Value = _value;
                        if (cacheTime < 0)
                        {
                            newDoCache.ExpiredTime = DateTime.MaxValue;
                        }
                        else
                        {
                            newDoCache.ExpiredTime = DateTime.Now.AddSeconds(cacheTime);
                        }
                        redisHelper.Set<string>(newDoCache.Id, newDoCache.Value);//设置缓存
                        this.dictCacheData[_uniqueKey] = newDoCache;
                       
                    }
                }
                catch { };
            }
        }
    }
}